package dao;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import dto.DateDTO;

import model.Folder;
import model.History;
import model.ServerLogEntry;
import model.User;
import enumerations.LogType;
import enumerations.Type;

/**
 * Session Bean implementation class HistoryDao
 * 
 * operatii de insert,delete,select pentru history
 * 
 * @author zahariem
 */
@Stateless
@LocalBean
public class HistoryDao {

	@PersistenceContext
	EntityManager em;

	@Resource
	UserTransaction ut;

	/**
	 * Default constructor.
	 */
	public HistoryDao() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * Insereaza in tabela history o inregistrare conform u datele primite ca
	 * parametru
	 * 
	 * @author zahariem
	 * @param idUser userul care face operatia
	 * @param idSource resursa pe care se face operatia
	 * @param typeReceived un String cu valoarea egala cu unul din elementele din enum.ul
	 *            {@link enumerations.Type}
	 * @param description este o descriere despre modificarea sau crearea resursei
	 * @param date data la care s-a efectuat op
	 */
	public void addChangeToHistory(int idUser, int idSource, Type typeReceived,String description, Date date) {
		try {
			if (typeReceived != null) {
				History h = new History();
				Folder f = new Folder();
				User u = new User();
				h.setDate(date);
				u = em.find(User.class, idUser);
				f = em.find(Folder.class, idSource);
				h.setFolder(f);
				h.setUser(u);
				h.setDescription(description);
				h.setType(typeReceived);
				em.persist(h);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Sterge toate inregistrarile din tabela history care au id_folder(de fapt
	 * id de resursa) egal cu parametrul primit
	 * 
	 * @author zahariem
	 * @param idSource id-ul resursei pentru care vreau sa sterg istoria
	 */
	public void removeHistory(int idSource) { // sterge toate inregistrarile pt
												// resursa cu id = idSource
		try {
			List<History> lh = em.createQuery("SELECT h FROM History h WHERE h.folder.id = :srcId",History.class)
					.setParameter("srcId", idSource)
					.getResultList();
			for (History h : lh) {
				History auxH = em.find(History.class, h.getId());
				em.remove(auxH);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * Returneaza toate inregistrarile pentru resursa cu id-ul primit ca
	 * parametru
	 * 
	 * @author zahariem
	 * @param idResource
	 *            este id-ul resursei
	 * @return o lista de obiecte de tip {@link model.History}
	 */
	public List<History> allHistoryToSource(int idResource) {
		
		List<History> lH = em.createQuery("SELECT h FROM History h WHERE h.folder.id = :srcId",History.class)
				.setParameter("srcId", idResource)
				.getResultList();

		return lH;
	}
	
	
	/**
	 *  Description: Creates query that returns server log entries between the dates
	 
	 * @author Zsombor
	 * @param  start  The date to start the search from
	 * @param  end	The end date for the search
	 * @return     The list of server log entries between the given parameters
	 */
	
	public List<ServerLogEntry> getQueryByDate(DateDTO start, DateDTO end) {

		String startd;
		String endd;

		startd = start.getYear() + "-" + start.getMonth() + "-"
				+ start.getDay();
		endd = end.getYear() + "-" + end.getMonth() + "-" + end.getDay();

	 	return em.createQuery("Select s from ServerLogEntry s where s.date between '" + startd
				+ "' AND '" + endd + "'",ServerLogEntry.class).getResultList();
	}
	
	/**
	 *  Description: Creates query that returns server log entries by type
	 
	 * @author Zsombor
	 * @param  query The query base
	 * @param  typeResult The enum types we need to add to the query
	 * @return     The list of server log entries by type
	 */
	
	public List<ServerLogEntry> getQueryByType(String query,List<LogType> typeResult){
		return em.createQuery(query, ServerLogEntry.class)
				.setParameter("types", typeResult).getResultList();
	}
	
	public List<ServerLogEntry> getAllTypes(String query, List<LogType> typeResult){
		return em.createQuery(query, ServerLogEntry.class)
				.setParameter("types", typeResult).getResultList();
	}
}
